<template>
  <div id="app">
    <!-- 页面头部，使用Element UI的Card组件包裹，增加样式层次感 -->
    <van-nav-bar
      title="订单详情"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    />
    <div class="order-detail-container">
      <el-row class="aa">
        <el-col :span="24" class="delivery-address">
          <div class="order-status" style="font-size: 20px">
            <van-tag type="danger" round v-if="order.status===1">待支付</van-tag>
            <van-tag type="danger" round v-if="order.status===2">待发货</van-tag>
            <van-tag type="danger" round v-if="order.status===3">待收货</van-tag>
            <van-tag type="danger" round v-if="order.status===4">已完成</van-tag>
            <van-tag type="danger" round v-if="order.status===5">已取消</van-tag>
          </div>
          <div class="exchange-note">
            <van-notice-bar
              left-icon="volume-o"
              text="兑换商品如无质量问题不支持退换货。"
            />
          </div>
        </el-col>
      </el-row>
      <el-row class="aa">
        <span class="address-title">收货地址</span>
        <el-col :span="24" class="delivery-address">
          <div class="">
            <span class="product-name">{{ order.addressName }},{{ order.tel }}</span>
            <span class="address-city">
                {{ order.province }} {{ order.city }} {{ order.county }} {{ order.addressDetail }}
              </span>
          </div>
        </el-col>
      </el-row>
      <el-row class="aa">
        <span class="exchange-goods-title">兑换商品</span>
        <el-col :span="24">
          <div class="product-image-container">
            <img :src="order.image" alt="product" class="product-image" height="100px">
            <span class="">{{ order.name }}</span>
            <span class="qq">{{ order.score }}积分</span>
          </div>
        </el-col>
      </el-row>
      <el-row  class="aa">
        <span class="exchange-goods-title">兑换商品</span>
        <el-col :span="24">
          <div class="price-summary">
            <div class="price-item">
              <span class="price-label">商品总价</span>
              <span class="price-value">{{ order.zong }}</span>
            </div>
            <div class="price-item">
              <span class="price-label">运费</span>
              <span class="price-value">0元</span>
            </div>
            <div class="price-item">
              <span class="price-label">商品实付</span>
              <span class="price-value">{{ order.payScore }}</span>
            </div>
          </div>
          <div class="price-summary">
            <div class="price-item">
              <span class="price-label">订单编号</span>
              <span class="price-value">{{ order.orderId }}</span>
            </div>
            <div class="price-item">
              <span class="price-label">创建时间</span>
              <span class="price-value">{{ order.createTime }}</span>
            </div>
          </div>
        </el-col>
      </el-row>
      <div class="order-buttons">
        <van-button type="info" size="small" text="取消订单" style="border-radius: 15px;" v-if="order.status===1" @click="cancelOrder()"/>
        <van-button type="danger" size="small" text="去支付" style="border-radius: 15px;" v-if="order.status===1" @click="goToPay()"/>
      </div>
    </div>
  </div>
</template>

<script>
import {Toast} from "vant";
import axios from "axios";

export default {
  data() {
    return {
      order: {},
    };
  },
  methods: {
    //取消订单
    cancelOrder() {
      // 取消订单的具体逻辑，比如发送请求到后端等
      this.findId = this.orderId
      console.log("findId=" + this.findId)
      axios.post(`/orders/orders/removeOrder?findId=${this.findId}`).then(res=>{
        this.$message.success(res.data.message)
      })
    },
    //去支付
    goToPay() {
      this.findId = this.orderId
      console.log("findId=" + this.findId)
      axios.get(`/orders/orders/findByOrderId?findId=${this.findId}`).then(res => {
        console.log(res.data.data.orders)
        localStorage.setItem("orderId", JSON.stringify(res.data.data.orders.orderId))
        this.$router.push('/orderSubmit02')
      })
    },
    onClickLeft() {
      Toast('返回');
      //返回上一个页面
      this.$router.push('orderPage');
    },
    findOrder() {
      axios.get(`/orders/orders/findByOrderId?findId=${this.orderId}`).then(res => {
        console.log(res.data.data.orders)
        this.order = res.data.data.orders
      })
    }
  },
  mounted() {
    var orderId = localStorage.getItem('orderId');
    if (orderId) {
      this.orderId = JSON.parse(orderId);
      console.log("orderId=", this.orderId);
    }
    this.findOrder();
  }
};
</script>

<style>
.aa {
  border-radius: 15px;
  background-color: white;
  padding: 20px;
  margin-bottom: 20px;
}

.order-detail-container {
  background-color: #dddddd;
  padding: 15px;
}

.address-label,
.product-label,
.price-label {
  display: block;
  margin-bottom: 5px;
  font-weight: bold;
}

.address-content {
  display: block;
  margin-bottom: 15px;
}

.product-name {
  display: block;
  margin-bottom: 5px;
}

.price-item {
  display: flex;
  justify-content: space-between;
  margin-bottom: 5px;
}

.order-buttons {
  display: flex;
  justify-content: flex-end;
  margin-top: 15px;
}
</style>
